<template>
  <div class="container">
    <!-- 表单筛选区域 -->

    <template>
      <el-row type="flex" justify="space-between" align="middle">
        <!-- 左侧 -->
        <div class="left" />
        <!-- 右侧 -->
        <div class="right">
          <el-button
            type="primary"
            plain
            size="small"
            icon="el-icon-plus"
            @click="$refs.addForm.dialogShow = true"
          >新增</el-button>
        </div>
      </el-row>
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="文字链接" name="first">  <br>
          <!-- 表格区域 -->
          <el-table :data="list" border>
            <el-table-column type="index" label="#" />
            <el-table-column prop="title" label="标题" />
            <el-table-column prop="des" label="链接描述" />
            <el-table-column prop="advLinknum" label="链接类型"><el-tag class="wzlx">文字类型</el-tag></el-table-column> />
            <el-table-column prop="url" label="链接地址" />
            <el-table-column label="操作">
              <template slot-scope="{ row }">
                <el-button
                  type="success"
                  plain
                  size="small"
                  icon="el-icon-edit"
                  @click="editRow(row.id)"
                >修改</el-button>
                <el-button
                  type="danger"
                  plain
                  size="small"
                  icon="el-icon-delete"
                  @click="delRow(row.id)"
                >删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <br>
          <!-- 分页区域 -->
          <el-pagination
            :current-page.sync="queryData.page"
            :page-sizes="[10, 20, 30, 40]"
            :page-size="queryData.limit"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalNum"
            background
            @size-change="sizeChange"
            @current-change="currentChange"
          /></el-tab-pane>
        <el-tab-pane label="图片链接" name="second">  <br>
          <!-- 表格区域 -->
          <el-table :data="list" border>
            <el-table-column type="index" label="#" />
            <el-table-column prop="des" label="链接描述" />
            <el-table-column prop="img" label="链接图片" />

            <el-table-column prop="advLinknum" label="链接类型"><el-tag class="wzlx">文字类型</el-tag></el-table-column> />
            <el-table-column prop="url" label="链接地址" />
            <el-table-column label="操作">
              <template slot-scope="{ row }">
                <el-button
                  type="success"
                  plain
                  size="small"
                  icon="el-icon-edit"
                  @click="editRow(row.id)"
                >修改</el-button>
                <el-button
                  type="danger"
                  plain
                  size="small"
                  icon="el-icon-delete"
                  @click="delRow(row.id)"
                >删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <br>
          <!-- 分页区域 -->
          <el-pagination
            :current-page.sync="queryData.page"
            :page-sizes="[10, 20, 30, 40]"
            :page-size="queryData.limit"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalNum"
            background
            @size-change="sizeChange"
            @current-change="currentChange"
          /></el-tab-pane>

      </el-tabs>

    </template>
    <el-form inline>

      <!-- 操作区域 -->

      <!-- 新增、修改弹框区域 -->

      <addForm ref="addForm" @updateList="initData" />
    </el-form>
  </div>
</template>
<script>
import {
  getAdvLinkListApi,
  updateAdvLinkInfoApi,
  delAdvLinkInfoApi

} from '@/api/adv_Link'
import addForm from './components/addFormLink.vue'
export default {
  name: 'AdvLink',
  components: {
    addForm
  },
  data() {
    return {
      list: [], // 表格数据
      totalNum: 0, // 总数
      // 列表的需要的参数
      queryData: {
        advLinkdesc: '',
        advLinkname: '',
        page: 1,
        limit: 10
      },
      activeName: 'second'
    }
  },
  created() {
    // 调用初始化
    this.initData()
  },
  methods: {
    handleClick(tab, event) {
      console.log(tab, event)
    },
    // 初始化数据
    async initData() {
      // 发起请求
      const res = await getAdvLinkListApi(this.queryData)
      // 数据可能需要加工
      // 赋值
      this.totalNum = res.count
      this.list = res.data
    },
    // 搜索提交
    onSubmit() {
      // 页码变成1
      this.queryData.page = 1
      // 拉取列表
      this.initData()
    },
    // 搜索重置
    onReset() {
      // 重置搜索表单
      this.queryData = {
        advLinkdesc: '',
        advLinkname: '',
        page: 1,
        limit: 10
      }
      // 拉取列表
      this.initData()
    },
    // 每页数量变化的时候
    sizeChange(newSize) {
      // newSize 最新的每页数量
      this.queryData.limit = newSize
      this.initData()
    },
    // 页码变化事件
    currentChange(newPage) {
      // newPage 最新的页码
      this.queryData.page = newPage
      this.initData()
    },
    // 修改事件
    async editRow(rowId) {
      // 获取改行的数据
      const res = await updateAdvLinkInfoApi({ id: rowId })
      // 赋给弹框里面的表单
      this.$refs.addForm.formData = res
      // 复杂点：一些负责的表单控件需要手动去回显
      // 打开弹框
      this.$refs.addForm.dialogShow = true
    },
    // 删除事件
    async delRow(rowId) {
      // 弹出确认框
      await this.$confirm('确认删除吗')
      // 发生删除请求
      await delAdvLinkInfoApi({ id: rowId })
      // 提示成功
      this.$message.success('删除成功')
      // 边缘问题：如果不是第一页，且这一页只有一个，且删除成功，页码需要-1
      if (this.queryData.page > 1 && this.list.length === 1) {
        this.queryData.page--
      }
      // 列表更新
      this.initData()
    }
  }
}
</script>
<style scoped>
.container {
  padding: 30px;

}
.right{
margin-top: 20px;
}
</style>
